#! /usr/bin/env python
# tovid-stats

"""Print statistical summaries of videos encoded with tovid, from data stored
in ~/.tovid/stats.tovid.
"""

# TODO:
#   * Output size, per minute of video, by format/bitrate/quantization
#   * Encoding time/min by format
#   * Encoding time/min by CPU speed

# TODO: Consider graphing (via layer.py)

import os
import sys
from libtovid import stats
from libtovid.utils import pretty_dict
from libtovid.layer import Scatterplot
from libtovid.mvg import Drawing

tovid_statfile = os.path.expanduser("~/.tovid/stats.tovid")

def get_statlist(statfile):
    # Open and read tovid stat file
    statfile = os.path.abspath(statfile)
    tempfile = "%s.csv" % statfile
    # Only use lines beginning with " (and not header line)
    os.system('cat "%s" | grep \'^ *\"[^T]\' > "%s"' % (statfile, tempfile))
    statlist = stats.Statlist(tempfile)
    os.remove(tempfile)
    return statlist

if __name__ == '__main__':
    args = sys.argv[1:]
    if len(args) == 0:
        print "This script gathers statistics from your ~/.tovid/stats.tovid"
        print "file, and can display summary information or average values."
        print " "
        print "Usage:"
        print "    tovid-stats COMMANDS"
        print "COMMANDS may be any of:"
        print "    -count FIELD"
        print "        Count total occurrences of distinct values in FIELD"
        print "    -average FIELD_1 [by FIELD_2]"
        print "        Average FIELD_1, optionally sorted by FIELD_2"
        print "    -list FIELD_1 by FIELD_2"
        print "        Show all values of FIELD_1, organized by FIELD_2"
        print "    -plot FIELD_1 by FIELD_2"
        print "        Like -list, but show a scatterplot image of the data."
        print "    -match FIELD VALUE"
        print "        Print statistics records where FIELD is VALUE"
        print "FIELDs may be any of the following:"
        for field in stats.fields:
            print "    %s" % field
        print "You may include multiple commands, for example:"
        print "    tovid-stats -count cpu_speed -average avg_bitrate by tvsys"
        sys.exit(0)

    statlist = get_statlist(tovid_statfile)

    # Parse command-line
    while args:
        arg = args.pop(0)
        if arg == '-count':
            field = args.pop(0)
            counts = statlist.count_unique(field)
            print "Total occurrences of each distinct value of '%s':" % field
            print pretty_dict(counts)
            print "Total of %s distinct values" % len(counts)
        elif arg == '-average':
            avg_field = args.pop(0)
            if args and args[0] == 'by':
                args.pop(0)
                by_field = args.pop(0)
                print "Average %s, sorted by %s:" % (avg_field, by_field)
                print pretty_dict(statlist.average_by(avg_field, by_field))
            else:
                print "Average %s:" % avg_field
                print statlist.average(avg_field)

        # Useful -list arguments:
        #    -list avg_bitrate by tgt_bitrate
        #    -list peak_bitrate by tgt_bitrate
        
        elif arg == '-list':
            list_field = args.pop(0)
            if args and args[0] == 'by':
                args.pop(0)
                by_field = args.pop(0)
                print "List of %ss by by %s:" % (list_field, by_field)
                print pretty_dict(statlist.list_by(list_field, by_field, True))
            else:
                print "Please use -list with a 'by' clause."

        elif arg == '-plot':
            list_field = args.pop(0)
            if args and args[0] == 'by':
                args.pop(0)
                by_field = args.pop(0)
                print "Generating a scatterplot of %ss by %s" % \
                      (list_field, by_field)
                xy_values = statlist.list_by(list_field, by_field, True)
                scatterplot = Scatterplot(xy_values, (640, 480), \
                                          by_field, list_field)
                drawing = Drawing((720, 576))
                drawing.fill('white')
                drawing.rectangle((0, 0), (720, 576))
                drawing.translate((40, 48))
                scatterplot.draw_on(drawing, 0)
                drawing.render()
        elif arg == '-match':
            field = args.pop(0)
            value = args.pop(0)
            if field in stats.int_fields:
                value = int(value)
            print "Finding statistics records where %s is %s" % (field, value)
            matches = statlist.get_matching(field, value)
            for match in matches:
                print "Matching record:"
                print pretty_dict(match)
            if matches == []:
                print "No matches found."
            else:
                print "Found %s records where %s is %s." % \
                      (len(matches), field, value)


def required_MB(format, tvsys, vbitrate, quant, seconds=1):
# Not working yet
    """Return the approximate number of Megabytes required to encode
    a video to the given format and tvsys, at the given video bitrate
    (in kbps) and quantization, for the given number of seconds. By default,
    returns MB per second of video.
    """
    statlist = get_statlist(tovid_statfile)
    
    # Determine final output size per second
    for record in statlist.records:
        if record['format'] == format and record['tvsys'] == tvsys and \
           record['tgt_bitrate'] == vbitrate:
            matches.append(record)

